package com.xju.subjectsystem.service.impl;

import com.xju.subjectsystem.mapper.BooksMapper;
import com.xju.subjectsystem.pojo.Books;
import com.xju.subjectsystem.pojo.Student;
import com.xju.subjectsystem.service.BooksService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: xju_-course_-kg_-master
 * @package: com.xju.subjectsystem.service.impl
 * @className: BooksServiceImpl
 * @author: Eiard
 * @description: TODO : 
 * @date: 2024-12-17 01:20
 */
@Service
public class BooksServiceImpl implements BooksService {

    private final BooksMapper booksMapper;

    public BooksServiceImpl(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Override
    public List<Books> selectByCsno(Integer csno) {
        return booksMapper.selectByCsno(csno);
    }


    @Override
    public void deleteByBno(Integer bno) {
        booksMapper.deleteByPrimaryKey(bno);
    }

    @Override
    public void add(Books books) {
        booksMapper.insert(books);
    }

    @Override
    public Map<String, List<String>> batchAddBooks(List<Books> booksList) {
        List<String> successList = new ArrayList<>();
        List<String> failList = new ArrayList<>();

        for (Books book : booksList) {
            try {
                this.add(book);
                successList.add("书籍：" + book.getBname() + " 导入成功");
            } catch (Exception e) {
                failList.add("书籍：" + book.getBname() + " 导入失败，原因：数据异常");
            }
        }

        // 将结果封装成Map返回
        Map<String, List<String>> resultData = new HashMap<>();
        resultData.put("successList", successList);
        resultData.put("failList", failList);


        return resultData;
    }

    public void updateSelective(Books books) {
        booksMapper.updateByPrimaryKeySelective(books);
    }

    @Override
    public void updateBooks(Books books) {
        Books books1 = new Books();
        books1.setBno(books.getBno());

        // 只能改书名和类别
        if (books.getBname() != null) {
            books1.setBname(books.getBname());
        }
        if (books.getCategory() != null) {
            books1.setCategory(books.getCategory());
        }

        this.updateSelective(books);
    }


}
